#include "SimpleAppender.h"

#include "Topic.h"
#include "recovery/fs/file/File.h"
#include "recovery/fs/file/FileAppender.h"

using recovery::Status;
using recovery::fs::file::FileAppender;
using recovery::fs::file::File;

SimpleAppender::SimpleAppender(Topic & topic)
: _topic(topic)
{
}

SimpleAppender::~SimpleAppender() {
}

bool SimpleAppender::open() {
	if(_topic.open() == 0){
		File f(_topic.logPath());
		if(f.exist()){
			return FileAppender::openExist(_topic.logPath(), _log);
		}
		else{
			return FileAppender::create(_topic.logPath(), _log);
		}
		return true;
	}
	return false;
}
bool SimpleAppender::close() {
	_log->close();
	_topic.close();
	return true;
};

std::pair<Status, size_t> SimpleAppender::append(const std::string & data){
	size_t appendedBytes = 0;
	const auto & s = _log->append(data + ";", appendedBytes);
	return std::make_pair(s.toStatus(), appendedBytes);
}
Status SimpleAppender::commit(){
	size_t appendedBytes = 0;
	const auto & s = _log->append("commit;", appendedBytes);
	if(s){
		const auto & s = _log->sync();
		return s ? s.toStatus() : s.toStatus("sync failed");
	}
	return s.toStatus("append commit info failed");
}
